package com.neusoft.databus.core.job.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.common.cache.CacheService;
import com.neusoft.databus.core.converter.CommandConverter;
import com.neusoft.databus.core.service.ExecuteCollectService;
import com.neusoft.databus.core.service.GatewayService;
import com.neusoft.databus.core.service.JobExecutableDetermineService;
import com.neusoft.databus.core.service.MetricGroupService;
import com.neusoft.databus.core.service.ResourceService;

/**
 * Quartz Scheduler configuration，自定义SchedulerFactoryBean，在具体job中可以拿到一些可用组件 <br>
 * 在QuartzJobBean实现类定义中，可以根据变量定义，自动拿到schedulerContext中值
 *
 * @author sunchf
 * @date 2018年12月26日 下午1:36:34
 */
@Component
public class SchedulerFactoryBeanCustomizerImpl implements SchedulerFactoryBeanCustomizer {

    @Autowired
    private GatewayService gatewayService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private JobExecutableDetermineService jobExecutableDetermineService;
    @Autowired
    private ExecuteCollectService executeCollectService;
    @Autowired
    private MetricGroupService metricGroupService;
    @Autowired
    private CommandConverter commandConverter;
    @Autowired
    private CacheService cacheService;

    @Override
    public void customize(final SchedulerFactoryBean schedulerFactoryBean) {
        final Map<String, Object> schedulerContext = new HashMap<>();
        schedulerContext.put("gatewayService", this.gatewayService);
        schedulerContext.put("resourceService", this.resourceService);
        schedulerContext.put("jobExecutableDetermineService", this.jobExecutableDetermineService);
        schedulerContext.put("executeCollectService", this.executeCollectService);
        schedulerContext.put("metricGroupService", this.metricGroupService);
        schedulerContext.put("commandConverter", this.commandConverter);
        schedulerContext.put("cacheService", this.cacheService);
        schedulerFactoryBean.setSchedulerContextAsMap(schedulerContext);
    }

}
